<template>
<div>
    <div style="text-align: left">
      <el-input class="userQuery"
                type="text"
          placeholder="请输入内容"
          size="small"
          style="width: 200px"
          v-model="userName"
          >
      </el-input>
      <el-button @click="initData" type="primary" icon="el-icon-search" size="small" v-show="this.hasPermission('user:query')">查询</el-button>
      <el-button type="primary" @click="show" icon="el-icon-plus" size="small" v-show="this.hasPermission('user:add')">新增</el-button>
      <el-button type="danger" @click="handleDeletes" icon="el-icon-delete" size="small" :disabled="this.multipleSelection.length==0" v-show="this.hasPermission('user:delete')">批量删除</el-button>
    </div>
    <div class="userTable">
      <el-table
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          :data="tableData"
          height="450"
          border
          ref="multipleTable"
          @selection-change="handleSelectionChange"
          style="width: 100%">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="100">
        </el-table-column>
        <el-table-column
            prop="realname"
            label="姓名"
            width="180">
        </el-table-column>
        <el-table-column
            prop="username"
            label="登陆名"
            width="180">
        </el-table-column>
        <el-table-column
            prop="sex"
            label="性别"
            :formatter="getSEX"
            width="100">
        </el-table-column>
        <el-table-column
            prop="role.rolename"
            label="所属角色"
            width="150">
        </el-table-column>
        <el-table-column
            prop="dept.deptname"
            label="所属部门"
            width="150">
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            width="150">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.status"
                :active-value="1"
                :inactive-value="0"
                active-color="#02538C"
                inactive-color="#B9B9B9"
                @change="changeSwitch(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column
            prop="lastlogin"
            label="最后登陆时间"
            width="180">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="250">
          <template slot-scope="scope">
            <el-button class="el-icon-search" @click="editClick(scope.row,false)" type="primary" size="small">查看</el-button>
            <el-button class="el-icon-edit" type="primary" @click="editClick(scope.row,false)" size="small">编辑</el-button>
            <el-button class="el-icon-delete"  @click="handleDelete(scope.row)" type="primary" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  <div class="page">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userVO.pageNum"
        :page-sizes="[20,100, 200, 300, 400]"
        :page-size="userVO.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="userVO.total">
    </el-pagination>
  </div>
<!-- 表单-->
  <el-dialog title="编辑用户信息" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form1" prop="form" :readOnly="this.readOnly">
      <el-input type="hidden" v-model="form.id"/>
      <el-form-item prop="realname" label="用户姓名" :label-width="formLabelWidth">
        <el-input placeholder="用户姓名" size="small" v-model="form.realname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="username" label="登陆名" :label-width="formLabelWidth">
        <el-input placeholder="登陆名" size="small"  v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="pwd" label="密码" :label-width="formLabelWidth">
        <el-input type="password" placeholder="密码" size="small"  v-model="form.pwd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="roleid" label="所属角色" :label-width="formLabelWidth">
        <el-select size="small"  v-model="form.roleid" placeholder="请选择角色" value-key="id">
          <el-option :label="role.rolename" :value="role.id"
                     v-for="(role) in this.roles" :key="role.id"></el-option>

        </el-select>
      </el-form-item>
      <el-form-item prop="deptid" label="所属部门" :label-width="formLabelWidth">
        <el-select size="small" value-key="id"  v-model="form.deptid" placeholder="请选择活动区域">
          <el-option :label="dept.deptname" :value="dept.id"
          v-for="(dept) in this.depts" :key="dept.id"></el-option>

        </el-select>
      </el-form-item>
      <el-form-item label="" :label-width="formLabelWidth">
        <el-switch
            v-model="form.status"
            active-value="1"
            active-text="启用"
            inactive-text="停用"
            inactive-value="0">
        </el-switch>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="msubmit">保 存</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
name: "Index",
  data() {

    return {
      readOnly:false,
      loading:true,
      multipleSelection: [],
      rules:{
        username:[{required:true,message:'用户名不能为空',trigger:'blur'}],
        realname:[{required:true,message:'姓名不能为空',trigger:'blur'}],
        pwd:[{required:true,message:'密码不能为空',trigger:'blur'}],
        roleid:[{required:true,message:'所属角色不能为空',trigger:'blur'}],
        deptid:[{required:true,message:'所属部门不能为空',trigger:'blur'}]
      },
      dialogFormVisible: false,
      formLabelWidth: '120px',
      userName:'',
      user:{
        param:{
          username:''
        },
        pageNumber:1,
        pageSize:20
      },
      userVO:{
        total:0,
        pageSize:20,
        pageNum:1
      },
      form:{
        realname:'',
        username:'',
        pwd:'',
        sex:'',
        deptid:'',
        roleid:'',
        status: '1'
      },
      depts:[],
      roles:[],
      tableData: []
    }
  },
  mounted() {
      this.initData();
  },

  methods: {
    getSEX(){
      for(var i in this.tableData){
        if(this.tableData[i].sex==1){
          return '男';
        }else{
          return '女';
        }
      }
    },
    handleClick(row) {
      console.log(row);
      },
    handleSizeChange(val) {
      this.user.pageSize = parseInt(val);
      this.initData();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.user.pageNumber=parseInt(val);
      this.initData();
      console.log(`当前页: ${val}`);
    },
    initData(){
      this.user.param.username=this.userName
      this.loading = true
      this.postRequest('/sys/user/query',this.user).then(resp=>{
        this.tableData = resp.result.list
        this.userVO=resp.result
        this.loading = false
      })
    },
    show(){
      this.getRequest('/sys/role?t='+new Date()).then(resp=>{
        if(resp.success){
          this.roles = resp.result;
        }

      })
      this.getRequest('/sys/dept/querys?t='+new Date()).then(resp=>{
        this.depts = resp.result;
      })
      this.dialogFormVisible = true
    },
    msubmit(){
      this.$refs.form1.validate(valid=>{
        if(valid){
          this.postRequest('/sys/user/save',this.form).then(resp=>{
            if(resp.success){
              this.$refs.form1.resetFields();
              this.dialogFormVisible = false;
              this.initData();

            }

          })
        }
      })


    },
    editClick(row,flag){
      this.readOnly=flag;
      this.getRequest('/sys/user/info/'+row.id).then(resp=>{
        if(resp.success){
          this.form = resp.result;
          this.form.status = this.form.status+'';
          this.show();
        }
      })
    },
    handleDelete(row){
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/sys/user/deletes?ids='+row.id).then(resp=>{
          if(resp.success){
            this.initData();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
  handleSelectionChange(val) {
      this.multipleSelection = val;

  },
    handleDeletes(){
      this.$confirm('此操作将永久删除'+this.multipleSelection.length+'个用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids='?'
        this.multipleSelection.forEach(item=>{
          ids+='ids='+item.id+'&'
        });
        this.deleteRequest('/sys/user/deletes'+ids).then(resp=>{
          if(resp.success){
            this.initData();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    changeSwitch(row){
      let user={id:row.id,status:row.status};
      this.postRequest('/sys/user/save',user).then(resp=>{
        if(resp.success){
          this.initData();
        }
      })
    },
    close(){
      this.$refs.form1.resetFields();
      this.dialogFormVisible = false;
    }

  }
}
</script>

<style scoped>
el-table{
  text-align: center;
}
.userQuery{
  margin-right: 10px;
}
.userTable{
  margin-top: 10px;
}
.page{
  margin-top: 10px;
  text-align: right;
}
</style>